#include <latch>
#include <iostream>
#include <thread>

void loadData(std::latch& latch) {
    // 模拟数据加载
    std::this_thread::sleep_for(std::chrono::seconds(1));
    std::cout << "Data loaded by thread " << std::this_thread::get_id() << std::endl;
    latch.count_down();  // 通知latch一个任务已完成
}

int main() {
    std::latch sync_point(3);  // 设置计数器为3，表示需要3个线程完成任务

    std::thread t1(loadData, std::ref(sync_point));
    std::thread t2(loadData, std::ref(sync_point));
    std::thread t3(loadData, std::ref(sync_point));

    sync_point.wait();  // 等待所有线程完成加载
    std::cout << "All data loaded, proceeding with main computation." << std::endl;

    t1.join();
    t2.join();
    t3.join();
    return 0;
}